#include "Quads.h"

Quad quad_gen(int op, Valeur* arg1, Valeur* arg2, Valeur* res){
    Quad new = (Quad)malloc(sizeof(StrQuad));
    new->op=op;
    new->arg1=arg1;
    new->arg2=arg2;
    new->res=res;

    return new;
}

void addQuadList(Quad* l, Quad quad){
	if(*l==NULL){
		*l = quad;
	}else{
        Quad traveler;
        for(traveler=*l; traveler->next!=NULL; traveler=traveler->next);
        traveler->next = quad;
    }
}

void freeQuadList(Quad l){
	int i;
	while(l!=NULL){
		Quad tmp = l;
        l = l->next;
		free(tmp);
	}
}

void print64(int a){
    char tab[11];
    tab[10]=0;
    unsigned char t='A';
    int i;
    for(i=9 ; i>=0 && a>0 ; i--){
        t=(unsigned char)a%64;
        a/=64;
        if(t<26)       t=t+'A';
        else if(t<52)  t=t+'a'-26;
        else if(t<62)  t=t+'0'-36;
        else if(t==62) t='+';
        else t='/';
        tab[i]=t;
    }
    printf("%s",tab+i+1);
}

void quad_print(Quad q){
    while(q != NULL){
        printf("QUAD : ");
        if(q->op){
            if(q->op<256)
                printf("%c ",q->op);
            else if(q->op==BOOLOP+VEE)
                printf("|| ");
            else if(q->op==BOOLOP+WEDGE)
                printf("&& ");
            else if(q->op==BOOLOP+NEG)
                printf("! ");
            else if(q->op==BOOLOP+POS)
                printf("pos ");
            else if(q->op==BOOLOP+EQ)
                printf("== ");
            else if(q->op==BOOLOP+NEQ)
                printf("!= ");
            else if(q->op==BOOLOP+LEQ)
                printf("<= ");
            else if(q->op==BOOLOP+GEQ)
                printf(">= ");
            else if(q->op==BOOLOP+LT)
                printf("< ");
            else if(q->op==BOOLOP+GT)
                printf("> ");
            else if(q->op==CONDOP+CIF)
                printf("IF ");
            else if(q->op==CONDOP+CEIF)
                printf("eIF ");
            else if(q->op==CONDOP+CELSE)
                printf("ELSE ");
            else if(q->op==CONDOP+CFI)
                printf("FI ");
            else if(q->op==CONDOP+CWHILE)
                printf("WHILE ");
            else if(q->op==CONDOP+CDO)
                printf("DO ");
            else if(q->op==CONDOP+CENDWHILE)
                printf("ENDWHILE ");
            else if(q->op==CONDOP+CFOR)
                printf("FOR ");
            else if(q->op==CONDOP+CENDFOR)
                printf("ENDFOR ");
            else
                printf("?(%d) ",q->op);
        }

        if(q->arg1 && q->arg1->name) printf("\033[34m%s\033[m ",q->arg1->name); 
        else if(q->arg1 && q->arg1->Specific.isConstant)    printf("%d ", q->arg1->Value.integer);
        else if(q->arg1){
            printf("\033[31m%p\033[m ",q->arg1);
        }

        if(q->arg2 && q->arg2->name) printf("\033[34m%s\033[m ",q->arg2->name); 
        else if(q->arg2 && q->arg2->Specific.isConstant)    printf("%d ", q->arg2->Value.integer);
        else if(q->arg2){
            printf("\033[31m%p\033[m ",q->arg2);
        }

        if(q->res){
            printf("\033[32m%p\033[m ",q->res);
        }
        printf("\n");
        q = q->next;
    }
    printf("\033[m");
}
